<?php
#                                                                                 
# author: Cody Sorgenfrey                                       
# (c)2012 South Hill Calvary Chapel                                               
#                                                                                 
include_once("./root.inc.php");
include_once("${root}/mysql/database_info.php");
include_once("${root}/mysql/common.php");
include_once("${root}/common.php");

function printStatement(){
	
	printf("<p>To preserve the purpose of the needs board, we ask that you only post legitimate needs to the needs board. Needs will be posted, only after being reviewed by the church staff. Your personal information will not be given out to others. Should someone email you about your need, the church is no longer responsible for moderating interactions. Feel free to <a href='mailto:webservant@southhillcalvary.org'>contact the webservant</a> to report abuse.</p>\n");
}

function printForm(){
	
	printf("<form action='/needsPost.php'>\n");
	
	printf("\t<input type='text' name='email' value='%s' placeholder='Your Email'/>\n", $_GET['email']);
	
	printf("\t<br><br><select name='cat'>\n");
	
	printf("<option value='#'>What type of need do you have?</option>\n");
	
	printf("<option value='furniture'>Furniture</option>\n");
	
	printf("<option value='clothes'>Clothes</option>\n");
	
	printf("<option value='hhItems'>House Hold Items</option>\n");
	
	printf("<option value='services'>Services/Labor</option>\n");
	
	printf("<option value='food'>Food</option>\n");
	
	printf("<option value='auto'>Automotive</option>\n");
	
	printf("<option value='housing'>Housing</option>\n");
		
	printf("<option value='misc'>Miscellaneous</option>\n");
	
	printf("</select><br>\n");
	
	printf("\t<br><input style='width:500px;' type='text' name='title' value='%s' placeholder='Post Title'/>\n", $_GET['title']);
	
	if ($_GET['summary']){
		
		printf("\t<br><textarea style='width:500px; height:400px;' name='summary'>%s</textarea>\n", $_GET['summary']);
	}
	else{
		
		printf("\t<br><textarea style='width:500px; height:400px;' name='summary'>Please summarize your need here.</textarea>\n");
	}
	printf("\t<br><input type='submit' value='Post' />\n");
	
	printf("</form><br>\n");

}

function checkValues(){
	
	if ($_GET['email']){
		
		if (strstr($_GET['email'], "@") && strstr($_GET['email'], ".")){
			
			$check = TRUE;
		}
		else {
			
			return "Invalid Email.";
		}
	}
	else return "Missing Email.";
	
	if ($_GET['cat']){
		
		if($_GET['cat'] != "#"){
			
			$check = TRUE;
		}
		else{
			
			return "Category cannot be \"What type of need do you have?\".";
		}
	}
	else return "Missing Need Category.";
	
	if ($_GET['title']) $check = TRUE;
	else return "Missing Title.";
	
	if ($_GET['summary']){
		
		if ($_GET['summary'] != "Please summarize your need here."){
			
			$check = TRUE;
		}
		else{
			
			return "Please fill out your need summary.";
		}
	} 
	else return "Missing prayer request/praise report.";
	
	if ($check == TRUE) return "TRUE";
}

function postNeedToDB(){

	$postDate = date("Y-m-d");
	
	mysql_query("INSERT INTO `needs` (title, summary, email, postDate, pending, category) VALUES ('".$_GET['title']."','".$_GET['summary']."','".$_GET['email']."','".$postDate."','1','".$_GET['cat']."')");
}

function emailForReview(){
	
	$message = "<html><body>From: ".$_GET['email']."<br><br>Title: ".$_GET['title']."<br>Summary: ".$_GET['summary']."<br><br><a href='http://www.southhillcalvary.org/needsReview.php?title=".urlencode($_GET['title'])."&cat=".$_GET['cat']."&email=".$_GET['email']."&approve=TRUE'>[click to approve]</a><a href='http://www.southhillcalvary.org/needsReview.php?title=".urlencode($_GET['title'])."&cat=".$_GET['cat']."&email=".$_GET['email']."&approve=FALSE'> [click to deny]</a></body></html>";
	
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";	
	$headers .= 'From: SHCC Needs Board <needs@southhillcalvary.org>' . "\r\n";
	
	return mail("codys@southhillcalvary.org", "Approval for needs board", $message, $headers);
}

function emailOfFailure(){
	
	$message = "<html><body>From: ".$_GET['email']."<br><br>Title: ".urlencode($_GET['title'])."<br>Summary: ".$_GET['summary']."<br><br><a href='http://www.southhillcalvary.org/needsPost.php?title=".$_GET['title']."&email=".$_GET['email']."&summary=".urlencode($_GET['summary'])."&cat=".$_GET['cat'].">[click to repost]</a></body></html>";
	
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	$headers .= 'From: SHCC Needs Board <needs@southhillcalvary.org>' . "\r\n";

	$checkEmail = mail("codys@southhillcalvary.org", "Post Failed", $message, $headers);
}

$title = "Post to Needs Board";
$meta_key='post, need, needs, ask, help';
$meta_desc="Post a need to the South Hill Calvary Chapel Needs Board.";
print_html_head($title ,$meta_key,$meta_desc);
print_java_head(null);

connect_select_test("needs");

begin_body();
page_title($title);

if ($_REQUEST['needsLogin'] == 1){

	if ($_GET['email'] || $_GET['cat'] || $_GET['title'] || $_GET['summary']){
			
		if (checkValues() == "TRUE"){
			
			postNeedToDB();
			
			if(emailForReview() == TRUE){
				
				printf("Your post has been sent for review and you will receive a conformation email upon approval/denial. We will try our best to get you the help you need as quickly as possible.");	
			}
			else{
							
				emailOfFailure();
				
				printf("Your post had some trouble sending, but is being reviewed and will be posted shortly.");	
			}
		}
		else {
			
			printStatement();
			
			printf("<h4><font color='red'>".checkValues()."</font></h4>");
			
			printForm();
		}
	}
	else {
		
		printStatement();
		
		printForm();
	}
}
else{
	
	printNeedsLogin("/needsPost.php", null);
}
end_page_title();
end_body();
?>